package com.itheima.web.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.itheima.domain.vo.UserAgeVo;
import com.itheima.util.SimpleAirQuality;
import com.itheima.util.SimpleWeather;
import com.itheima.web.manager.HomeManager;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HomeController {
    @Autowired
    private HomeManager homeManager;

    /**
     * 用户年龄分布
     */
    @GetMapping("/manager/users/age")
    public Map<String, Integer> findByAge() {
        return homeManager.findByAge();
    }


    /**
     * 用户性别分布
     */
    @GetMapping("/manager/users/gender")
    public Map<String, Integer> findByGender() {
        return homeManager.findByGender();
    }

    /**
     * 查询天气预报
     */
    @GetMapping("/weather/{city}")
    public Map<String, JSONObject> checkWeather(
            @PathVariable(value = "city") String city
    ) {
        System.out.println(city);
        JSONObject jsonObject = SimpleWeather.queryWeather(city);
        Map<String, JSONObject> map = new HashMap<>();
        map.put("queryWeather", jsonObject);
        return map;
    }

    /**
     * 查询空气质量
     */
    @GetMapping("/airQuality/{city}")
    public Map querryAirQuality(
            @PathVariable(value = "city") String city
    ) {
        String queryAirQuality = SimpleAirQuality.queryAirQuality(city);
        String str1 = queryAirQuality.substring(0, queryAirQuality.indexOf("]"));
        String str2 = str1.substring(1, str1.length());
        //json字符串转Map
        Map maps = (Map) JSON.parse(str2);
        System.out.println("map:" + maps);
        //JSONObject jsonObject = JSONObject.fromObject(queryAirQuality);
        Map<String, Map> map = new HashMap<>();
        map.put("querryAirQuality", maps);
        return map;
    }

}